package edu.vt.ece3574.ninjatag;

import java.util.HashMap;
import java.util.Map;

import org.clear.client.http.HttpUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;

import com.facebook.android.Utility;


/**
 * This is a quick abstraction for the PlayerIf interface
 * which implements recording the player name and pic to avoid
 * needing to query the database multiple times.
 * @author Chris Dusold
 *
 */
public abstract class APlayer implements PlayerIf{
	private String mName = null;
	private Bitmap mPic = null;
	private String mScore = null;
	private String mId = null;
	private long timeOfScoreRefresh = -1;
	//private GPSIf mGPS = null;
	private static final long TIME_TO_REFRESH = 60000;
	//TODO: add saving between sessions.
	
	public APlayer(String id){
		mId=id;
	}

    public APlayer(String name, Bitmap pic, String id) {
		mName=name;
		mPic=pic;
		mId=id;
	}
    
    public String getID() {
    	return mId;
    }
    
    public Long getUID() {
    	return Long.valueOf(mId.substring(1));
    }

	public boolean save() {
		String url = Utility.SERVER + "/game/newUser";
		Map<String, Object> data = new HashMap<String, Object>();
        data.put("name", mName);
        data.put("uid", mId);
        //addSaveValues(data);
		try {
			JSONObject json = new JSONObject(HttpUtils.postData(url, data));
			return json.get("token").equals(mId);
		} catch (JSONException e) {
			e.printStackTrace();
			return false;
		}
    }
	
	public boolean setGPSCoordinates(GPSIf gpsCoords) {
		String url = Utility.SERVER + "/game/set/location";
		Map<String, Object> data = new HashMap<String, Object>();
        data.put("uid", mId);
        data.put("location", gpsCoords);
		try {
			JSONObject json = new JSONObject(HttpUtils.postData(url, data));
			return !json.get("location").equals("Failed to add");
		} catch (JSONException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public GPSIf getGPSCoordinates() {
		String url = Utility.SERVER + "/game/get/location";
		Map<String, Object> data = new HashMap<String, Object>();
        data.put("uid", mId);
		try {
			JSONObject json = new JSONObject(HttpUtils.postData(url,data));
			Object response = json.get("location");
			if (response.equals("Failed to get")) {
				return null;
			}
			return (GPSIf) json.get("location");
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static boolean load() {
		return false;
	}
    
    public abstract void addSaveValues(Editor editor);
	
	@Override
	public String getName() {
		if (mName == null) {
			mName = queryName();
		}
		return mName;
	}
	
	public abstract String queryName();

	@Override
	public String getScore() {
		if (mScore == null || System.currentTimeMillis()-timeOfScoreRefresh<TIME_TO_REFRESH) {
			mScore = queryScore();
			timeOfScoreRefresh=System.currentTimeMillis();
		}
		return mScore;
	}

	public abstract String queryScore();
	
	@Override
	public Bitmap getPicture() {
		if (mPic == null) {
			mPic = queryPicture();
		}
		return mPic;
	}
	
	public abstract Bitmap queryPicture();

} //TODO: Do note, this base has not been tested, nor is it finished.
